package xyz.hubery.listener;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.hubery.model.Constants;
import xyz.hubery.pojo.Orders;
import xyz.hubery.service.OrdersService;

/**
 * 延迟队列消费者，检查订单状态
 */
@Service
@RocketMQMessageListener(topic = Constants.CHECK_ORDER_STATUS_TOPIC,
        consumerGroup = "check_orders_queue"
)
public class CheckOrdersListener implements RocketMQListener<String> {
    @Autowired
    private OrdersService ordersService;

    @Override
    public void onMessage(String orderId) {
        // 查询订单
        Orders orders = ordersService.findById(orderId);
        // 如果此时订单状态还是未支付，则将状态改为交易关闭
        if (orders.getStatus() == 1) {
            orders.setStatus(6);
            ordersService.update(orders);
        }
    }
}
